package navigation.results.cluster.next;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.lexer.Lexer;
import org.htmlparser.lexer.Page;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import wish.stages.ParsingStage;

public class NextFinder {

	public class NextFinderRunnable implements Runnable{

		private static final String HREF_ATTRIBUTE = "href";
		private File file;
		private BufferedWriter bw;

		public NextFinderRunnable(File file, BufferedWriter bw) {
			this.file = file;
			this.bw = bw;
		}

		@Override
		public void run() {
			
			
			try {
				
				String htmlContent = ParsingStage.readFile(file);
				
				Parser parser = new Parser(new Lexer(new Page(htmlContent, "UTF-8")));
				
				NodeList n = parser.parse(new HasAttributeFilter(HREF_ATTRIBUTE));
				
				for (int i = 0; i < n.size(); i++) {
					
					String textString = ParsingStage.recoverText(n.elementAt(i),false).toLowerCase();
					
					if (textString.trim().length() == 0)
						continue;
					
					List<String> text = generateSet(textString);
					
					if ((text.size()<=2) && ((text.contains("next") || text.contains("more"))) || (text.size()==0 && textString.contains(">"))){
						
						TagNode t = (TagNode)n.elementAt(i);
						
//						String hyperLink = t.getAttribute(HREF_ATTRIBUTE).toLowerCase();
						
//						if (hyperLink.contains("="+query)){
						bw.write("<br>");
						bw.write(n.elementAt(i).toHtml());
//						}
					}
					
				}
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			
		}

		private List<String> generateSet(String textString) {
			
			List<String> ret = new ArrayList<String>();
			 
			String[] st = textString.split("\\W");
			
			for (int i = 0; i < st.length; i++) {
				
				ret.add(st[i].toLowerCase());
				
			}
			
			return ret;
		}
		
	}
	
	private static Set<String> toProcess;

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		String type = "training";
		
		toProcess = loadToProcess("/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/" + type);
		
		String navFolder = "/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/Navigation/" + type + "/";
		
		File outp = new File("/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/NavigationNext/palmieri/" + type + "/palmieri.html");
		
		File fold = new File(navFolder);
		
		File[] navfolders = fold.listFiles(new FileFilter() {
			
			@Override
			public boolean accept(File pathname) {
				return toProcess.contains(pathname.getName());
			}
		});		

		BufferedWriter bw = new BufferedWriter(new FileWriter(outp));
		
		Arrays.sort(navfolders,new Comparator<File>() {

			@Override
			public int compare(File o1, File o2) {
				
				return Double.compare(Double.valueOf(o1.getName()), Double.valueOf(o2.getName()));
				
			}
		});
		
		for (int i = 0; i < navfolders.length; i++) {
			
			System.out.println("DATABASE: " + i + " -> " + navfolders[i].getName());
			
			bw.write("<h2> Database: "+navfolders[i].getName()+"</h2>");
			
			File folder = navfolders[i];
			
			File[] files = folder.listFiles(new FileFilter() {
				
				@Override
				public boolean accept(File pathname) {
					return pathname.getAbsolutePath().endsWith(".html");
				}
			});
			
			for (int j = 0; j < files.length; j++) {
				
				new NextFinder().execute(files[j],bw);
				
			}
			
		}
		
		bw.close();
		
	}

	private void execute(File file, BufferedWriter bw) {
		
		Thread t = new Thread(new NextFinderRunnable(file,bw));
		
		t.start();
		
		try {
			t.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private static Set<String> loadToProcess(String fileName) throws IOException {
		
		Set<String> set = new HashSet<String>();
		
		BufferedReader br = new BufferedReader(new FileReader(fileName));
		
		String line;
		
		while((line=br.readLine())!=null){
			
			set.add(line);
			
		}
		
		br.close();
		
		return set;
		
	}
	
	private static List<String> loadQueries(File file) throws IOException {
		
		List<String> ret = new ArrayList<String>();
		
			
		BufferedReader br = new BufferedReader(new FileReader(file));
		
		String line;
		
		while ((line=br.readLine())!=null){
			
			String[] l = line.split(" ");
			
			ret.add(l[0]);
			
		}
		
		br.close();
		
		return ret;
		
	}
	
}
